package meiyunting.shiwuzhaoling.service.impl;

import meiyunting.shiwuzhaoling.model.Claim;
import meiyunting.shiwuzhaoling.model.ClaimStatus;
import meiyunting.shiwuzhaoling.model.ClaimType;
import meiyunting.shiwuzhaoling.model.Item;
import meiyunting.shiwuzhaoling.model.ItemStatus;
import meiyunting.shiwuzhaoling.model.User;
import meiyunting.shiwuzhaoling.repository.ClaimRepository;
import meiyunting.shiwuzhaoling.repository.ItemRepository;
import meiyunting.shiwuzhaoling.service.ClaimService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClaimServiceImpl implements ClaimService {

    private final ClaimRepository claimRepository;
    private final ItemRepository itemRepository;

    public ClaimServiceImpl(ClaimRepository claimRepository, ItemRepository itemRepository) {
        this.claimRepository = claimRepository;
        this.itemRepository = itemRepository;
    }

    @Override
    public Claim createClaim(Item item, User claimer, String description) {
        return createClaim(item, claimer, description, ClaimType.CLAIM_FOUND, null);
    }
    
    @Override
    public Claim createClaim(Item item, User claimer, String description, ClaimType claimType, String imagePath) {
        // 检查用户是否已经认领过该物品
        if (hasUserClaimedItem(item, claimer)) {
            throw new RuntimeException("您已经提交过该物品的申请");
        }

        Claim claim = new Claim();
        claim.setItem(item);
        claim.setClaimer(claimer);
        claim.setDescription(description);
        claim.setStatus(ClaimStatus.PENDING);
        claim.setClaimTime(LocalDateTime.now());
        claim.setClaimType(claimType);
        claim.setImagePath(imagePath);

        return claimRepository.save(claim);
    }

    @Override
    public List<Claim> getClaimsByItem(Item item) {
        return claimRepository.findByItem(item);
    }

    @Override
    public List<Claim> getClaimsByUser(User user) {
        return claimRepository.findByClaimer(user);
    }

    @Override
    public Claim getClaimById(Long id) {
        return claimRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("认领记录不存在"));
    }

    @Override
    public Claim updateClaimStatus(Long id, ClaimStatus status) {
        Claim claim = getClaimById(id);
        claim.setStatus(status);
        claim.setProcessedTime(LocalDateTime.now());
        return claimRepository.save(claim);
    }

    @Override
    @Transactional
    public Claim approveClaimAndCompleteItem(Long id) {
        // 获取认领记录
        Claim claim = getClaimById(id);
        
        // 更新认领状态为已批准
        claim.setStatus(ClaimStatus.APPROVED);
        claim.setProcessedTime(LocalDateTime.now());
        
        // 更新物品状态为已完成
        Item item = claim.getItem();
        item.setStatus(ItemStatus.COMPLETED);
        item.setClaimedBy(claim.getClaimer());
        item.setClaimedAt(LocalDateTime.now());
        
        // 保存物品和认领记录
        itemRepository.save(item);
        
        // 拒绝其他认领请求
        List<Claim> otherClaims = claimRepository.findByItem(item);
        for (Claim otherClaim : otherClaims) {
            if (!otherClaim.getId().equals(id) && otherClaim.getStatus() == ClaimStatus.PENDING) {
                otherClaim.setStatus(ClaimStatus.REJECTED);
                otherClaim.setProcessedTime(LocalDateTime.now());
                claimRepository.save(otherClaim);
            }
        }
        
        return claimRepository.save(claim);
    }

    @Override
    public boolean hasUserClaimedItem(Item item, User user) {
        return claimRepository.existsByItemAndClaimer(item, user);
    }
} 